Skip to content

lis2mdl: Add two-point temperature calibration.#114

Merged
nedseb merged 2 commits intomainfrom
fix/lis2mdl-temp-calibration
Mar 14, 2026
Merged

lis2mdl: Add two-point temperature calibration.#114
nedseb merged 2 commits intomainfrom
fix/lis2mdl-temp-calibration

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 14, 2026

Summary

  • Add calibrate_temperature() method for two-point calibration (gain + offset)
  • Separate hardware base offset (_temp_base_offset = 25°C) from user calibration (_temp_gain, _temp_offset)
  • Update set_temp_offset() to reset gain to 1.0
  • Add mock test scenarios for offset and two-point calibration

The LIS2MDL has a specific temperature model: T = base_offset + raw / sensitivity where
base_offset defaults to 25°C (empirical, not guaranteed by datasheet). The calibration
applies on top: T_cal = gain * T_factory + offset.

Closes #113

Test plan

Mock tests (CI)

python3 -m pytest tests/ -k "lis2mdl and mock" -v

Hardware tests (STeaMi board)

python3 -m pytest tests/ --port /dev/ttyACM0 -k "lis2mdl and hardware" -s -v
  • ruff check passes
  • All mock tests pass (8 tests including 2 new calibration tests)
  • Hardware validation on STeaMi board

Test results

$ python3 -m pytest tests/ --port /dev/ttyACM0 -k "lis2mdl and hardware" -s -v

======================================================= test session starts ========================================================
platform linux -- Python 3.13.7, pytest-8.3.5, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/nedjar/sandbox/micropython-steami-lib
configfile: pytest.ini
plugins: typeguard-4.4.2
collected 161 items / 152 deselected / 9 selected                                                                                  

tests/test_scenarios.py::test_scenario[board_i2c_scan/LIS2MDL WHO_AM_I (0x1E)/hardware] 0x40
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Verify WHO_AM_I register/hardware] 0x40
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read WHO_AM_I via method/hardware] 0x40
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read status register/hardware] 255
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Magnitude in plausible range/hardware] 204.70
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature in plausible range/hardware] 24.38
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Soft reset then WHO_AM_I/hardware] 0x40
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Reboot then WHO_AM_I/hardware] 0x40
PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Magnetic field values feel correct/hardware]   Magnitude: 0.21 µT
  Temperature: 24.50 °C
  Heading: 202.57 °
  [MANUAL] Ces valeurs sont-elles cohérentes (magnitude ~25-65µT, cap 0-360°) ? [Entree=oui / Echap=non] 
PASSED

================================================ 9 passed, 152 deselected in 49.11s ================================================

@nedseb nedseb requested a review from Copilot March 14, 2026 11:09
@nedseb nedseb self-assigned this Mar 14, 2026
@nedseb nedseb added the enhancement New feature or request label Mar 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds two-point temperature calibration support to the LIS2MDL driver to align with the calibration capabilities available in other sensor drivers, and introduces mock scenario coverage for the new calibration behavior.

Changes:

  • Add _temp_gain, _temp_offset, and _temp_base_offset and apply them in read_temperature_c().
  • Add calibrate_temperature(ref_low, measured_low, ref_high, measured_high) for two-point gain/offset calibration.
  • Extend LIS2MDL mock scenarios with offset and two-point calibration tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/lis2mdl/lis2mdl/device.py Introduces temperature gain/offset calibration and updates temperature conversion pipeline.
tests/scenarios/lis2mdl.yaml Adds mock scenarios validating single-point offset and two-point calibration behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/lis2mdl/lis2mdl/device.py Outdated
Comment thread tests/scenarios/lis2mdl.yaml Outdated
@nedseb
Copy link
Copy Markdown
Contributor Author

nedseb commented Mar 14, 2026

Addressed Copilot review:

  • set_temp_offset() is now an additive offset (consistent with WSEN-HIDS/WSEN-PADS): T_cal = T_factory + offset
  • _temp_base_offset remains fixed at LIS2MDL_TEMP_OFFSET (25°C) and is never modified by user calibration
  • Updated test to use -2.0 offset (delta), matching the convention in other driver test scenarios

@nedseb nedseb merged commit 5b1e157 into main Mar 14, 2026
5 checks passed
@nedseb nedseb deleted the fix/lis2mdl-temp-calibration branch March 14, 2026 12:15
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.0.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lis2mdl: Add two-point temperature calibration.

2 participants